package cn.edu.hcnu.servlet;

import cn.edu.hcnu.entity.EmpEntity;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri=request.getRequestURI();
        uri=uri.substring(1);

        if("login".equals(uri)){
            dologin(request,response);
        }else  if("exit".equals(uri)){
            HttpSession session=request.getSession();
            session.invalidate();
            response.sendRedirect("login.html");
        }


    }
    public  void dologin(HttpServletRequest request,HttpServletResponse response){
        String id=request.getParameter("username");
        String passwd=request.getParameter("password");

        String url="jdbc:oracle:thin:localhost:1512:orcl";
        String username ="scott";
        String password="password";

        Connection conn=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        String sql="SELCET * FROM t_users where user_id=? and password=?";

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn= DriverManager.getConnection(url,username,password);
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,id);
            pstmt.setString(2,passwd);
            List emplist=new ArrayList();
            rs=pstmt.executeQuery();
            if(rs.next()){
                sql="select * from emp";
                pstmt=conn.prepareStatement(sql);
                rs=pstmt.executeQuery();
                while (rs.next()){
                    EmpEntity emp=new EmpEntity();
                    emp.setEmpno(rs.getInt("empno"));
                    emp.setEname(rs.getString("ename"));
                    emp.setJob(rs.getString("JOB"));
                    emp.setMgr(rs.getInt("MGR"));
                    emp.setHiredate(rs.getDate("HIREDATE"));
                    emp.setSal(rs.getFloat("SAL"));
                    emp.setComm(rs.getFloat("COMM"));
                    emp.setDeptno(rs.getInt("DEPTNO"));
                    emplist.add(emp);
                }
                HttpSession session=request.getSession();
                request.setAttribute("empdata",emplist);
                session.setAttribute("username",id);
                request.getRequestDispatcher("index.jsp").forward(request,response);
            }else {
                response.sendRedirect("login.html");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                pstmt.close();
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }
}
